package com.yycx.module.bus.provider.handler.process;

import com.alibaba.fastjson.util.TypeUtils;
import com.yycx.bpm.provider.common.handler.ProcessStartHandler;
import com.yycx.bpm.provider.model.ProcessInstanceRequest;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.utils.OrderNoUtil;
import com.yycx.module.bus.client.entity.BusDrugsPlan;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.provider.service.BusDrugsPlanService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 启动资金支付审核流程
 *
 * @author zyf
 */
@Component
public class DrugsPlanPaymentPayStartProcessHandler implements ProcessStartHandler {

    @Autowired
    private BusPaymentService paymentService;
    @Resource
    private OrderNoUtil orderNoUtil;
    @Resource
    private BusPaymentService busPaymentService;
    @Autowired
    private BusDrugsPlanService busDrugsPlanService;

    /**
     * 初始化流程参数
     *
     * @param processInstanceRequest
     * @return
     */
    @Override
    public String init(ProcessInstanceRequest processInstanceRequest) {
        Long companyId = OpenHelper.getCompanyId();
        processInstanceRequest.setProcessKey("payment_pay_" + companyId);
        Long userId = OpenHelper.getUserId();
        String businessKey = processInstanceRequest.getBusinessKey();
        BusDrugsPlan busDrugsPlan = busDrugsPlanService.getById(businessKey);
        BusPayment busPayment = new BusPayment();
        busPayment.setTitle("药品采购预付款");
        busPayment.setAmountType("DrugsPlan");
        BigDecimal planAmount = busDrugsPlan.getPlanAmount();
        BigDecimal advancePayment = busDrugsPlan.getAdvancePayment();
        Boolean gt = FlymeUtils.gt(planAmount, advancePayment);
        //3预付款审核开始 4预付款审核结束
        //5差额付款审核开始 6差额付款付款审核结束
        //7全额付款审核开始 8全额付款付款审核结束
        //6/8审核结束
        Integer checkState = busDrugsPlan.getCheckState();
        if (checkState == 2) {
            if (gt) {
                busPayment.setAmount(busDrugsPlan.getAdvancePayment());
                busDrugsPlan.setCheckState(3);
            } else {
                busPayment.setAmount(busDrugsPlan.getPlanAmount());
                busDrugsPlan.setCheckState(7);
            }
        }
        if (checkState == 4) {
            busDrugsPlan.setCheckState(5);
            busPayment.setAmount(busDrugsPlan.getReturnAmount());
        }
        busDrugsPlanService.updateById(busDrugsPlan);
        busPayment.setOrderId(busDrugsPlan.getDrugsPlanId().toString());
        busPayment.setPaymentState(0);
        String orderNo = orderNoUtil.getIncrementNum();
        busPayment.setPaymentNo("CG" + orderNo);
        busPayment.setUserId(userId);
        busPayment.setNickName(OpenHelper.getNickName());
        busPayment.setSupplierId(busDrugsPlan.getSupplierId());
        busPaymentService.save(busPayment);
        processInstanceRequest.setBusinessKey(TypeUtils.castToString(busPayment.getPaymentId()));
        return processInstanceRequest.getBusinessKey();
    }

    @Override
    public Map<String, Object> initVariables(ProcessInstanceRequest processInstanceRequest, ProcessDefinition definition) {
        Map<String, Object> map = new HashMap<>();
        map.put("nqbz", 0);
        return map;
    }

    /**
     * 启动完成业务处理
     *
     * @param processInstanceRequest
     * @param definition
     * @param processInstance
     * @param tasks
     * @param variables
     */
    @Override
    public Boolean afterComplete(ProcessInstanceRequest processInstanceRequest, ProcessDefinition definition, ProcessInstance processInstance, List<Task> tasks, Map<String, Object> variables) {
        String businessKey = processInstanceRequest.getBusinessKey();
        BusPayment busPayment = paymentService.getById(businessKey);
        String orderId = busPayment.getOrderId();
        BusDrugsPlan busDrugsPlan = busDrugsPlanService.getById(orderId);
        String processInstanceId = processInstance.getProcessInstanceId();
        busDrugsPlan.setProcessInstanceId(processInstanceId);
        busDrugsPlanService.updateById(busDrugsPlan);
        //更新资金支付单为已提交并关联流程实例ID
        return paymentService.updatePaymentState(Long.parseLong(businessKey), processInstance.getProcessInstanceId(), CommonConstants.ENABLED);
    }
}
